<?php
namespace Controller;

class MasterController {

	/**
	 * @return String HTML document
	 */
	public static function doControll() {
		$xhtml = "";

		$database = new \Model\Database();
		$database -> Connect(new \Model\DBConfig());
		$loginhandler = new \Model\LoginHandler($database);
		$logincontroller = new LoginController();
		$regcontroller = new RegisterController();
		$postview = new \View\PostView();
		$navview = new \View\NavigationView();
		$pageview = new \Common\Page();
		$navView = new \View\NavigationView();
		$posthandler = new \Model\PostHandler($database);
		$commenthandler = new \Model\CommentHandler($database);
		$postController = new PostController();
		$postListController = new PostListController();
		$postnewcontroller = new PostNewController();
		$login = $logincontroller -> DoControll($loginhandler);

		//Kollar om man kollar på ett inlägg eller inte
		if ($navView -> isViewingpost()) {
			$pco = $postController -> doControll($posthandler, $database);
			$xhtml = $pco -> m_postHtml;
		} else {
			$postListController = new PostListController();
			$xhtml = $postListController -> doControll($database);

		}
		//Kollar vad användaren vill göra.
		if (isset($_GET['page'])) {
			if ($_GET['page'] == 'register') {
				//registerformulär
				$xhtml = $regcontroller -> DoControll($database);
			} else if ($loginhandler -> IsLoggedIn() == true) {

				if ($_GET['page'] == 'newpost') {
					$xhtml = $postnewcontroller -> DoControll($database);
				}
			}
		}
		//Stänger databasen
		$database -> Close();

		//Lägger till en stylesheet
		$stylesheet = $pageview -> AddStyleSheet('Css/style.css');

		//Returnerar titel och xhtml till pageView
		return $pageview -> GetHTMLPage("edport.se", $xhtml, $login);
	}

}
